/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean;

import com.jb.framework.PersistentContext;

import java.util.List;

import com.jb.faces.component.NamingContainer;


public class TableRow extends HtmlItem implements NamingContainer {
    static final long serialVersionUID = 123123L;

    static {
        publish(com.jb.web.bean.TableRow.class, GENERAL, "cols");
    }

    private int cols = 5;

    public TableCell getCellAt(int loc) {
        return (TableCell) this.getComponentAt(loc);
    }

    public void setCols(int newCols) {
     
        if (newCols != cols) {
            cols = newCols;
        }
    }

    public boolean isChildAccepatble(BaseComponent component) {
        return (component instanceof TableCell);
    }
    
    public int getCols() {
        return cols;
    }

    public void setDefault(PersistentContext pContext) {
        if (getTable() != null) {
            cols = getTable().getColumns();
        }

        setStyleClass("jbTableRow");
        valueChanged(pContext);
    }

    public Table getTable() {
        if (getParent() instanceof Table) {
            return (Table) getParent();
        } else {
            return null;
        }
    }

    public void delete(PersistentContext ctx) {
        super.delete(ctx);
        if(this.getTable()!=null)
            this.getTable().setRows(getTable().getRows() - 1);
    }

    public void onPaste(Object pasted) {
        if (pasted instanceof TableCell) {
            this.cols++;
        }
    }

    public boolean valueChanged(PersistentContext ctx) {
        List cells = getChildrenByType(TableCell.class);
        int newCols = cells.size();

        if (newCols < getCols()) {
            for (int i = newCols; i < getCols(); i++) {
                TableCell cell = this.createNewCell();
                cell.setDefault(ctx);
             //   cell.setId(getId() + i);
                cell.setSequence((i + 1) * 10);
                addChild(cell);
            }
        } else {
            int newsize = cells.size() - getCols();

            if (newsize > 0) {
                int i = getCols();

                while (i < cells.size()) {
                    TableCell cell = (TableCell) cells.get(i);
                    removeChild(cell);
                    i++;
                }
            }
        }

        return true;
    }

    protected TableCell createNewCell() {
        return new TableCell();         
    }
    protected void parentAcceptable(BaseComponent parent)
        throws ChildTypeNotAllowedException {
        if (parent instanceof Grid) {
            return;
        }

        throw new ChildTypeNotAllowedException("Tablerow can only be added to table");
    }
}
